#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <strings.h>
#include <cstring>

#include "clientTypes.h"

char send_buf[1024];
char recv_buf[1024];
struct sockaddr_in addr;

void send_object(int sock, Serializable* object);

int main(int argc, char** argv) {
	int sock;
	
	if (argc < 2) {
		printf("client usage error: missing destination ip address\n");
		return(-1);
	}
	
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("initialization of socket failed");
		exit(errno);
	}
	
	bzero(&addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(6006);
	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
		
	inet_aton(argv[1], (struct in_addr*)&addr.sin_addr.s_addr);
	A a(100);
	B b(200);
	C c(300);
	
	send_object (sock, &a);
	send_object (sock, &b);	
	send_object (sock, &c);
	
	close(sock);
	return 0;
}

void send_object(int sock, Serializable* object) {
	std::string str = object->serialize();
	memcpy(send_buf, str.c_str(), str.length());
	sendto(sock, (void *)send_buf, sizeof(send_buf), 0, 
			(struct sockaddr *)&addr, (socklen_t)sizeof(addr));
}
